package ncov.web;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import ncov.model.*;
import ncov.service.DepartmentService;
import ncov.service.GradeService;
import ncov.service.StaffService;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WuChangJian
 * @date 2020/6/7 9:49
 */
public class StaffAction extends ActionSupport implements ModelDriven<Staff> {
    private Logger logger = Logger.getLogger(LocalAuthAction.class);
    private Result result;
    private Staff staff = new Staff();
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private StaffService staffService;

    public String getStaffInfo() {
        Map<String, Object> modelMap = new HashMap<>();
        HttpSession session = ServletActionContext.getRequest().getSession();
        LocalAuth localAuth = (LocalAuth) session.getAttribute("localAuth");
        Staff staff = localAuth.getStaff();
        if (staff != null) {
            Department dept = departmentService.getDeptById(staff.getDeptId());
            modelMap.put("localAuth", localAuth);
            modelMap.put("staff", staff);
            // 级联查询到的前台无关字段无需传递
            dept.setGrades(null);
            modelMap.put("department", dept);
            result = new Result();
            result.setSuccess(true);
            result.setData(modelMap);
        }
        return SUCCESS;
    }

    /**
     * 查询某职工下管理的所有班级
     * @return
     */
    public String listGrades() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        LocalAuth localAuth = (LocalAuth) session.getAttribute("localAuth");
        Staff staff = localAuth.getStaff();
        if (staff != null) {
            List<Grade> gradesByStaffId = gradeService.getGradesByStaffId(staff.getStaffId());
            result = new Result();
            result.setData(gradesByStaffId);
            result.setSuccess(true);
        }
        return SUCCESS;
    }

    public String updateProfile() {
        logger.info("staff profile --> " + staff);
        result = new Result();
        HttpSession session = ServletActionContext.getRequest().getSession();
        LocalAuth localAuth = (LocalAuth) session.getAttribute("localAuth");
        Staff newSf = localAuth.getStaff();
        Staff sf = staffService.update(newSf,staff);
        if (sf != null) {
            result.setData(sf);
            result.setSuccess(true);
        } else {
            result.setSuccess(false);
            result.setErrMsg("内部系统错误！");
        }
        return SUCCESS;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    @Override
    public Staff getModel() {
        return staff;
    }
}
